home *** CD-ROM | disk | FTP | other *** search
- Path: news.uni-jena.de!news
- From: mkt@isun04.inf.uni-jena.de (Tilo Koerbs)
- Newsgroups: comp.lang.c++
- Subject: Re: Help with Bug Pleeeeease!
- Date: 31 Jan 1996 13:53:36 GMT
- Organization: Lehrstuhl fuer Rechnerarchitektur- und kommunikation, FSU Jena
- Message-ID: <4ens90$qlr@fsuj01.rz.uni-jena.de>
- References: <tday-2801961838030001@tday.slip.netcom.com>
- Reply-To: mkt@isun04.inf.uni-jena.de
- NNTP-Posting-Host: isun07.inf.uni-jena.de
-
- I found one error:
-
- String String::operator+(const String & st) const
- {
- String temp1;
- delete [] temp1.str;
- temp1.len = st.len+len;
- temp1.str = new char[temp1.len+1];
- strcpy(temp1.str,str);
- for (int i=len; i< temp1.len; i++)
- temp1.str[i]=st.str[i-len];
- temp1.str[temp1.len+1]='\0'; // temp1.str[temp1.len] is the last
- // byte in temp1.str!!!
- cout << temp1.len << " length1st\n";
- return temp1;
- }
- And why don't you use strcat?
- Like: strcpy(temp1.str, str);
- strcat(temp1.str, st.str); // That's enough!
- return temp1;
-
- Another hint for this function:
- String String::operator+(const char * s) const
- {
- String tempo;
- delete [] tempo.str;
- tempo.len=strlen(s);
- tempo.str = new char[tempo.len+1];
- strcpy(tempo.str,s);
- String temp = *this+tempo;
- cout << temp.len << " length2nd\n";
- return temp;
- }
- I would write this like:
- String String::operator+(const char * s) const
- {
- return *this + String(s);
- }
-
- To the question of the temporaries:
- Normally these are destroyed after the statement is executed.
- But of course, there is no need for the compiler to destroy a temporary
- befor exit of the current block. But after exiting the
- current block, the temporaries should have been deleted!
-
- I hope you code will be running soon!
- Bye.
-
-